﻿using DXBase;
using DXBase.D2D;
using DXBase.DWrite;
using DXBase.Scenes;
using DXBase.Structs;
using Windows.Foundation;
using Windows.UI;

namespace Sample02_D2D_DPI 
{
	public class D2D_DPIScene : ModelBase, IScene
	{
		public InputController Inputcontroller { get; set; }

		public float Dpi
		{
			get { return context != null ? context.Dpi : DPI.InchToDIP; }
			set
			{
				if (context != null)
				{
					context.Dpi = value;
					OnPropertyChanged();
				}
			}
		}
		public DXContext Context
		{
			get { return context; }
			set 
			{
				if (value == context)
					return;
				context = value;
				OnPropertyChanged();
				OnPropertyChanged("Dpi");
			}
		}
		DXContext context;

		SolidColorBrush bBlack;
		SolidColorBrush bGray;
		TextLayout lText, lLineInfo;
		TextFormat txtFormat24;

		public D2D_DPIScene()
		{
			bBlack = new SolidColorBrush { Color = Colors.Black };
			bGray = new SolidColorBrush { Color = Colors.LightGray };

			lText = new TextLayout(
				"72 pt font, no transforms!",
				new TextFormat() { FontSize = 72, },
				768, 100);
			lLineInfo = new TextLayout(
				"96 (DIP) \"Pixels\"",
				new TextFormat() { FontSize = 12, },
				96, 96);

			txtFormat24 = new TextFormat() { FontSize = 24 };
		}

		public void Update(DXContext ctxt, PerformanceTimer ptimer)
		{
			Context = ctxt;
		}

		public void Render(DXContext ctxt)
		{
			var g = new D2DGraphics(ctxt);
			g.Clear((Color4F)Colors.Beige);

			var p = new Vector2F(96, 200);
			var pDown = p  + new Vector2F(0, 96);

			// draw big font message
			g.DrawTextLayout(p, lText, bBlack);
			g.DrawLine(p, pDown, bBlack, 2, null);
			g.DrawLine(p + new Vector2F(-5, 0), p + new Vector2F(5, 0), bBlack, 2, null);
			g.DrawLine(pDown + new Vector2F(-5, 0), pDown + new Vector2F(5, 0), bBlack, 2, null);

			// draw info message
			var ps = ctxt.PixelSize;
			var ss = ctxt.Size;
			g.DrawText("Screen Size is", txtFormat24, new Rect(pDown + new Size(0, 32), new Size(200, 200)), bBlack);
			g.DrawText(string.Format("  in Pixels: {0}x{1}", ps.width, ps.height), txtFormat24, new Rect(pDown + new Size(0, 64), new Size(200, 200)), bBlack);
			g.DrawText(string.Format("  in DIP:    {0}x{1}", (int)ss.Width, (int)ss.Height), txtFormat24, new Rect(pDown + new Size(0, 96), new Size(200, 200)), bBlack);

			// draw little 96px line
			g.Transform = Matrix3x2F.Rotation(-90, pDown);
			g.DrawTextLayout(pDown - new Vector2F(-5, 20), lLineInfo, bBlack);
			g.Transform = Matrix3x2F.Identity();
		}
	}
}
